package com.kaesun.ufit.tools;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.kaesun.ufit.weight.R;

public class SummaryView extends View {

	private Paint mPaintYellowText = new Paint();
	private Paint mPaintWhiteLText = new Paint();
	private Paint mPaintWhiteRText = new Paint();
	private Paint mPaintWhiteLText2 = new Paint();
	private Paint mPaintWhiteRText2 = new Paint();

	private Paint mPaintSliderIn = new Paint();
	private Paint mPaintBitmap = new Paint();
	private Bitmap mSliderIn = null;

	private float mArchievement = 0;	// 0 ~ 150 [%]
	private float mWeight[] = {0, 0, 0};	// [kg]
	private float mKCalroie[] = {0, 0, 0};	// [Kcal]
	private int mCount = 0;	// [th]
	private int mSet = 0;	// [th]

	public SummaryView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

		init();
	}

	public SummaryView(Context context, AttributeSet attrs) {
		super(context, attrs);

		init();
	}

	public SummaryView(Context context) {
		super(context);

		init();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		//super.onDraw(canvas);

		canvas.drawText(String.format("%d", (int)mArchievement), 350, 310, mPaintYellowText);

		{
			final int _SX = 77, _EX = 524, _Y = 381, _IMG = 13;
			final float rate = (float)((_EX - _SX) / 150.);

			float cx = (mArchievement * rate);
			if (_IMG < cx) {
				canvas.drawLine(_SX, _Y, cx+_SX-_IMG, _Y, mPaintSliderIn);
			}
			if (null != mSliderIn) {
				canvas.drawBitmap(mSliderIn, cx+_SX-_IMG, _Y-_IMG, mPaintBitmap);
			}
		}

		canvas.drawText(String.format("%02d", mCount), 260, 665, mPaintWhiteLText);	// count
		canvas.drawText(String.format("%02d", mSet), 340, 665, mPaintWhiteRText);	// set

		canvas.drawText(String.format("%02d", (int)mWeight[0]), 260, 850, mPaintWhiteLText);	// weight
		canvas.drawText(String.format("%3.2f", mKCalroie[0]), 340, 850, mPaintWhiteRText);		// calorie
		if (0 == mArchievement) {
			if (2 <= mSet) {
				canvas.drawText(String.format("%02d", (int)mWeight[1]), 260, 920, mPaintWhiteLText2);	// weight
				canvas.drawText(String.format("%3.2f", mKCalroie[1]), 340, 920, mPaintWhiteRText2);		// calorie
			}
			if (3 <= mSet) {
				canvas.drawText(String.format("%02d", (int)mWeight[2]), 260, 990, mPaintWhiteLText2);	// weight
				canvas.drawText(String.format("%3.2f", mKCalroie[2]), 340, 990, mPaintWhiteRText2);		// calorie
			}
		}
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {

		super.onSizeChanged(w, h, oldw, oldh);
	}


	public void setValue(float archievement, float weight, float calroie, int count, int set) {
		mArchievement = Math.min(archievement, 150);
		mWeight[0] = weight;
		mKCalroie[0] = (calroie / 1000);
		mCount = count;
		mSet = set;

		invalidate();
	}
	public void setValue(float archievement, float weight[], float calroie[], int count, int set) {
		mArchievement = Math.min(archievement, 150);
		for (int i=0; i<weight.length; i++) {
			mWeight[i] = weight[i];
			mKCalroie[i] = (calroie[i] / 1000);
			if (0 < mKCalroie[i]) {
			}
		}
		mCount = count;
		mSet = set;

		invalidate();
	}


	private void init() {
		mPaintYellowText.setAntiAlias(true);
		mPaintYellowText.setColor(Color.YELLOW);
		mPaintYellowText.setTextSize(100);
		mPaintYellowText.setTextAlign(Paint.Align.RIGHT);

		mPaintWhiteLText.setAntiAlias(true);
		mPaintWhiteLText.setColor(Color.WHITE);
		mPaintWhiteLText.setTextSize(100);
		mPaintWhiteLText.setTextAlign(Paint.Align.RIGHT);

		mPaintWhiteRText.setAntiAlias(true);
		mPaintWhiteRText.setColor(Color.WHITE);
		mPaintWhiteRText.setTextSize(100);
		mPaintWhiteRText.setTextAlign(Paint.Align.LEFT);

		mPaintWhiteLText2.setAntiAlias(true);
		mPaintWhiteLText2.setColor(Color.WHITE);
		mPaintWhiteLText2.setTextSize(80);
		mPaintWhiteLText2.setTextAlign(Paint.Align.RIGHT);

		mPaintWhiteRText2.setAntiAlias(true);
		mPaintWhiteRText2.setColor(Color.WHITE);
		mPaintWhiteRText2.setTextSize(80);
		mPaintWhiteRText2.setTextAlign(Paint.Align.LEFT);

		mPaintSliderIn.setAntiAlias(true);
		mPaintSliderIn.setColor(Color.WHITE);
		mPaintSliderIn.setStyle(Paint.Style.STROKE);
		mPaintSliderIn.setStrokeCap(Paint.Cap.ROUND);
		mPaintSliderIn.setStrokeWidth(5);

		mSliderIn = BitmapFactory.decodeResource(getResources(), R.drawable.img_circle_slider_big);
	}
}
